package com.sat.pay.service.Impl;

import com.github.wxpay.sdk.WXPayUtil;
import com.sat.common.entity.HttpClient;
import com.sat.pay.service.WxPayService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WxPayServiceImpl implements WxPayService {


    // 在 application.yml 里面有，直接注入即可
    @Value("${weixin.appid}")
    private String appid;
    @Value("${weixin.partner}")
    private String partner;
    @Value("${weixin.partnerkey}")
    private String partnerkey;
    @Value("${weixin.notifyurl}")
    private String notifyurl;




    @Override
    public Map<String, String> wxPaynative(String body, String outTradeNo, Integer totalFee) {
        try {
            // Set the parameters required for a unified order
            Map<String, String> map = new HashMap<>();
            map.put("appid", appid); // 公众账号ID
            map.put("mch_id", partner); // 商户号
            map.put("nonce_str", WXPayUtil.generateNonceStr()); // 随机字符串
            map.put("body", body); // 商品描述
            map.put("out_trade_no", outTradeNo); // 订单号
            map.put("total_fee", totalFee + ""); // 标价金额
            map.put("spbill_create_ip", "127.0.0.1"); // 终端IP
            map.put("notify_url", notifyurl); // 通知地址
            map.put("trade_type", "NATIVE"); // 交易类型: NATIVE扫码支付
            String xmlJson = WXPayUtil.generateSignedXml(map, partnerkey);


            // Call wx unified ordering interface
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            httpClient.setHttps(true);

            // Required request parameters: 公共 + 特有
            httpClient.setXmlParam(xmlJson); //
            httpClient.post();

            // Returns the argument after the access
            String xmlContent = httpClient.getContent();
            // xml to mapper
            Map<String, String> returntMap = WXPayUtil.xmlToMap(xmlContent);
            return returntMap;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    public Map<String, String> payStatus(String outTradeNo) {

        try {
            // Set the parameters required for a unified order
            Map<String, String> map = new HashMap<>();
            map.put("appid", appid); // 公众账号ID
            map.put("mch_id", partner); // 商户号
            map.put("nonce_str", WXPayUtil.generateNonceStr()); // 随机字符串
            map.put("out_trade_no", outTradeNo); // 订单号
            String xmlJson = WXPayUtil.generateSignedXml(map, partnerkey);


            // Call wx unified ordering interface
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            httpClient.setHttps(true);

            // Required request parameters: 公共 + 特有
            httpClient.setXmlParam(xmlJson); //
            httpClient.post();

            // Returns the argument after the access
            String xmlContent = httpClient.getContent();
            // xml to mapper
            Map<String, String> returntMap = WXPayUtil.xmlToMap(xmlContent);
            return returntMap;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    public Map<String, String> closeorder(String outTradeNo) {
        try {
            // Set the parameters required for a unified order
            Map<String, String> map = new HashMap<>();
            map.put("appid", appid); // 公众账号ID
            map.put("mch_id", partner); // 商户号
            map.put("nonce_str", WXPayUtil.generateNonceStr()); // 随机字符串
            map.put("out_trade_no", outTradeNo); // 订单号
            String xmlJson = WXPayUtil.generateSignedXml(map, partnerkey);


            // Call wx unified ordering interface
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/closeorder");
            httpClient.setHttps(true);

            // Required request parameters: 公共 + 特有
            httpClient.setXmlParam(xmlJson); //
            httpClient.post();

            // Returns the argument after the access
            String xmlContent = httpClient.getContent();
            // xml to mapper
            Map<String, String> returntMap = WXPayUtil.xmlToMap(xmlContent);
            return returntMap;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }
}
